package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.OperateLog;
import org.example.system.entity.Respond;
import org.example.system.service.OperateLogService;
import org.example.system.service.RespondService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 响应控制层
 */
@RestController
@RequestMapping("/app/respond")
@Slf4j
public class RespondController {
    @Autowired
    private RespondService respondService;

    /**
     * 按条件分页查询
     * @param respond
     * @return
     */
    @GetMapping("/list")
    public R list(Respond respond){
        IPage<Respond> page = respondService.selectPage(respond);
        log.info("------------ RespondController | list | selectPage | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }

    /**
     * 添加响应
     * @param respond
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Respond respond, HttpServletRequest request){
        log.info("--------- RespondController | add | input : {}", JSON.toJSON(respond));
        int row = respondService.add(respond);
        if(row <= 0){
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }

    /**
     * 根据id查询响应数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id){
        log.info("--------- RespondController | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(respondService.getOne(id));
    }
}
